package main;

import java.util.ArrayList;

import network.ServerConnection;
import database.DatabaseAPI;

public class Main {
	private static Config config = null;
	private static DatabaseAPI dbApi = null;
	
	private static ServerConnection connection = null;
	
	public static ArrayList<Room> roomList = null;
	
	public static void main(String[] args) {
		
		//save all the status
		doShutDownWork();
		
		config = new Config("./config.xml");
		config.load();
		
		dbApi = new DatabaseAPI(config);
		dbApi.openDatabase();
		dbApi.printRecords();
		
		roomList = new ArrayList<Room>();
		
		connection = new ServerConnection(config);
		connection.openConnection();
	}
	
	/**
	 * protection work when shun down?
	 */
	public static void doShutDownWork() {
		Runtime.getRuntime().addShutdownHook(new Thread() {//add an action before the system is shut down
			public void run() {
				config.save();
				dbApi.closeDatabase();
				
				System.out.println("The server is shut down!");
			}
		});
	}
	
	public static Config getConfig() {
		return config;
	}
	
	public static DatabaseAPI getDbAPI() {
		return dbApi;
	}
	
}
